package validate;

import burp.IExtensionHelpers;
import burp.IHttpRequestResponse;
import burp.IRequestInfo;
import org.apache.commons.lang3.StringUtils;
import ui.Model;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by shuaidong on 2017/6/25.
 */
public class Validate implements IValidate{
    public static String hostBlackList = Model.getInstanceModel().getHostBlackList();
    public static String fileSuffixBlackList = Model.getInstanceModel().getFileSuffixBlackList();
    public static String hostWhiteList = Model.getInstanceModel().getHostWhiteList();
    public static String fileWhiteList = Model.getInstanceModel().getFileWhiteList();
    public static boolean iswhiteOn = false;
    public static boolean isblackOn = true;
    private IExtensionHelpers helpers;

    public Validate(IExtensionHelpers helpers) {
        this.helpers = helpers;

    }

    public Validate() {
    }

    public boolean patchBlackList(IHttpRequestResponse  iHttpRequestResponse) {
        if (StringUtils.isEmpty(hostBlackList)&&StringUtils.isEmpty(fileSuffixBlackList)){
            return false;
        }
        IRequestInfo requestInfo = helpers.analyzeRequest(iHttpRequestResponse);
        String host = requestInfo.getUrl().getHost();
        String path = requestInfo.getUrl().getPath();
        String[] strings = path.split("\\.");
        String fileSuffix = "";
        try {
             fileSuffix = strings[1];
        } catch (ArrayIndexOutOfBoundsException e) {
            fileSuffix = "";
        }
        if (!StringUtils.isEmpty(hostBlackList)&&StringUtils.isEmpty(fileSuffixBlackList)){
            return patchPattern(hostBlackList,host);
        }
        if (StringUtils.isEmpty(hostBlackList)&&!StringUtils.isEmpty(fileSuffixBlackList)){
            return patchPattern(fileSuffixBlackList,fileSuffix);
        }

        if (!StringUtils.isEmpty(hostBlackList)&&!StringUtils.isEmpty(fileSuffixBlackList)){
            return patchPattern(hostBlackList,host)|patchPattern(fileSuffixBlackList,fileSuffix);
        }


        return false;
    }

    public boolean patchWhiteList(IHttpRequestResponse iHttpRequestResponse) {
        if (StringUtils.isEmpty(hostWhiteList)&&StringUtils.isEmpty(fileWhiteList)){
            return false;
        }
        IRequestInfo requestInfo = helpers.analyzeRequest(iHttpRequestResponse);
        String host = requestInfo.getUrl().getHost();
        String path = requestInfo.getUrl().getPath();
        String[] strings = path.split("\\.");
        String fileSuffix = "";
        try {
            fileSuffix = strings[1];
        } catch (ArrayIndexOutOfBoundsException e) {
            fileSuffix = "";
        }
        if (!StringUtils.isEmpty(hostWhiteList)&&StringUtils.isEmpty(fileWhiteList)){
            return patchPattern(hostWhiteList,host);
        }
        if (StringUtils.isEmpty(hostWhiteList)&&!StringUtils.isEmpty(fileWhiteList)){
            return patchPattern(fileWhiteList,fileSuffix);
        }

        if (!StringUtils.isEmpty(hostWhiteList)&&!StringUtils.isEmpty(fileWhiteList)){
            return patchPattern(hostWhiteList,host)|patchPattern(fileWhiteList,fileSuffix);
        }

        return false;
    }

    public  boolean patchPattern(String regex,String string){
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        return matcher.find();
    }






}
